/////////////////////////////////////////////////////////////////////////
// MIT License
//
// Copyright (c) 2000-2011 OpenNETCF Consulting, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
////////////////////////////////////////////////////////////////////////

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.Win32;
using Microsoft.Win32;

namespace OpenNETCF.Windows.Forms.Calendar
{
    internal class UIUtils
    {
        private static Image m_imgGradientBG;
        private static int m_cxCurrent, m_cyCurrent;
        public static Color ColorGradientStartEven = Color.FromArgb(255, 255, 255);
        public static Color ColorGradientEndEven = Color.FromArgb(181, 207, 247);
        public static Color ColorGradientStartOdd = Color.FromArgb(255, 255, 255);
        public static Color ColorGradientEndOdd = Color.FromArgb(214, 227, 247);
        
        public static void PaintWatermark(Graphics g, int width, int height)
        {
            string evalString = "Evaluation version";
            Brush evalBrush = new SolidBrush(Color.Red);
            Font evalFont = new Font("Arial", 14, FontStyle.Italic | FontStyle.Bold);

            SizeF size = g.MeasureString(evalString, evalFont);
            g.DrawString(evalString, evalFont, evalBrush, (width - (int)size.Width) / 2, (height - (int)size.Height) / 2);

            evalBrush.Dispose();
            evalFont.Dispose();
        }

        public static Image GetGradientBG(int cx, int cy)
        {
            return GetGradientBG(cx, cy, false);
        }
        public static Image GetGradientBG(int cx, int cy, bool force)
        {
            if (cx == m_cxCurrent && cy == m_cyCurrent && m_imgGradientBG != null && !force)
                return m_imgGradientBG;

            if (m_imgGradientBG != null)
                m_imgGradientBG.Dispose();
            m_cxCurrent = cx;
            m_cyCurrent = cy;
            try
            {
                RegistryKey keyColors = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Color");
                int[] clrs;
                byte[] clrByte = keyColors.GetValue("SHColor") as byte[];
                clrs = new int[clrByte.Length >> 2];
                Buffer.BlockCopy(clrByte, 0, clrs, 0, clrByte.Length & 0xfc);

                ColorGradientStartEven = ColorFromSystemColor(clrs[36]);
                ColorGradientStartOdd = ColorFromSystemColor(clrs[15]);
                ColorGradientEndEven = ColorFromSystemColor(clrs[37]);
                ColorGradientEndOdd = ColorFromSystemColor(clrs[16]);
            }
            catch { }
            m_imgGradientBG = GenerateGradientBG(m_cxCurrent, m_cyCurrent, ColorGradientStartEven, ColorGradientStartOdd, ColorGradientEndEven, ColorGradientEndOdd);

            return m_imgGradientBG;
        }

        public static Color ColorFromSystemColor(int sysColor)
        {
            return Color.FromArgb(sysColor & 0xff, (sysColor >> 8) & 0xff, (sysColor >> 16) & 0xff);
        }

        public static void ListViewSetGradient(ListView lv)
        {
            Win32Window.SendMessage(lv.Handle, (int)LVM.SETEXTENDEDLISTVIEWSTYLE, LVS_EX_GRADIENT, LVS_EX_GRADIENT);
        }
        private const int LVS_EX_GRADIENT = 0x20000000;

        public static Image GenerateGradientBG(int cx, int cy, Color clrStart1, Color clrStart2, Color colorEnd1, Color colorEnd2)
        {
            Bitmap bmEven = new Bitmap(cx, 1);
            Bitmap bmOdd = new Bitmap(cx, 1);
            int r01 = clrStart1.R, g01 = clrStart1.G, b01 = clrStart1.B;
            int r02 = clrStart2.R, g02 = clrStart2.G, b02 = clrStart2.B;
            int r11 = colorEnd1.R, g11 = colorEnd1.G, b11 = colorEnd1.B;
            int r12 = colorEnd2.R, g12 = colorEnd2.G, b12 = colorEnd2.B;
            for (int i = 0; i < cx; i++)
            {
                Color clrEven = Color.FromArgb((r01 * (cx - i) + r11 * i) / cx, (g01 * (cx - i) + g11 * i) / cx, (b01 * (cx - i) + b11 * i) / cx);
                Color clrOdd = Color.FromArgb((r02 * (cx - i) + r12 * i) / cx, (g02 * (cx - i) + g12 * i) / cx, (b02 * (cx - i) + b12 * i) / cx);
                bmEven.SetPixel(i, 0, clrEven);
                bmOdd.SetPixel(i, 0, clrOdd);
            }
            Bitmap bmBlock = new Bitmap(cx, 4);
            Graphics gx = Graphics.FromImage(bmBlock);
            gx.DrawImage(bmEven, 0, 0);
            gx.DrawImage(bmEven, 0, 1);
            gx.DrawImage(bmOdd, 0, 2);
            gx.DrawImage(bmOdd, 0, 3);
            gx.Dispose();
            bmEven.Dispose();
            bmOdd.Dispose();

            Bitmap bmOut = new Bitmap(cx, cy);
            gx = Graphics.FromImage(bmOut);
            for (int i = 0; i < cy; i += 4)
                gx.DrawImage(bmBlock, 0, i);

            gx.Dispose();
            return bmOut;
        }
    }
}
